/*******************************************************************************
 * CBMoreNewsTableViewCell.m
 * ComicsBlog
 *
 * Adrien Bougouin <adrien.bougouin@gmail.com>
 */

#import "CBMoreNewsTableViewCell.h"

@implementation CBMoreNewsTableViewCell

- (id) init {
  NSUInteger cellStyle = UITableViewCellStyleDefault;

  self = [super initWithStyle:cellStyle reuseIdentifier:@"more_news"];

  if(self) {
    // add a new autorelease pool to the stack
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *resources = [mainBundle bundlePath];
    float ratio = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? CBMoreNewsIPadRatio: 1.0;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    CGRect imageFrame = CGRectMake(0,
                                   0,
                                   CGRectGetWidth(applicationFrame) / ratio,
                                   CBMoreNewsHeight);
    NSString *backgroundUSelFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                                       resources,
                                                                       CBMoreNewsBackgroundUSelImage];
    UIImage *backgroundUSel = [[UIImage alloc] initWithContentsOfFile:backgroundUSelFilePath];
    NSString *backgroundSelFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                                      resources,
                                                                      CBMoreNewsBackgroundSelImage];
    UIImage *backgroundSel = [[UIImage alloc] initWithContentsOfFile:backgroundSelFilePath];
    NSString *labelFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                               resources,
                                                               CBMoreNewsLabelImage];
    UIImage *label = [[UIImage alloc] initWithContentsOfFile:labelFilePath];

    _backgroundImageView = [[UIImageView alloc] initWithFrame:imageFrame];
    _labelImageView = [[UIImageView alloc] initWithFrame:imageFrame];

    [_backgroundImageView setImage:backgroundUSel];
    [_backgroundImageView setHighlightedImage:backgroundSel];
    [_backgroundImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [_labelImageView setImage:label];
    [_labelImageView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [[self contentView] addSubview:_backgroundImageView];
    [[self contentView] addSubview:_labelImageView];

    // memory management
    [backgroundUSelFilePath release];
    [backgroundUSel         release];
    [backgroundSelFilePath  release];
    [backgroundSel          release];
    [labelFilePath          release];
    [label                  release];

    // delete the local autorelease pool then dealloc autoreleased objects used
    // by the method
    [pool release];
  }

  return self;
}

- (void) dealloc {
  [super dealloc];

  [_backgroundImageView release];
  [_labelImageView release];
}

- (void) startSpinning {
  NSBundle *mainBundle = [NSBundle mainBundle];
  NSString *resources = [mainBundle bundlePath];
  NSMutableArray *spinnerSequence = [[NSMutableArray alloc] init];

  for(int i = 1; i <= 8; ++i) {
    NSString *imageFilePath = [[NSString alloc] initWithFormat:@"%@/%@_%d.png",
                                                               resources,
                                                               CBMoreNewsSpinnerImagePrefix,
                                                               i];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageFilePath];

    [spinnerSequence addObject:image];
    [spinnerSequence addObject:image];

    // memory management
    [imageFilePath  release];
    [image          release];
  }

  [_labelImageView setAnimationImages:spinnerSequence];
  [_labelImageView startAnimating];

  // memory management
  [spinnerSequence release];
}

- (void) stopSpinning {
  [_labelImageView setAnimationImages:nil];
}

@end

